<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Messenger/classes/PhpFox_ComponentMassMail.class.php, $Author:  $ */



/** Mass Mail component
 *
 * @package module.messenger
 * @version $Id: PhpFox_ComponentMassMail.class.php 146 2008-01-09 00:24:45Z phpfoxsv $
 *   PHPFox Team ()
 */
class PhpFox_Mod_Messenger_ComponentMassMail extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
	    $bProc = false;
	    $iTo = 0;
	    $oReq = &App::getRequest();
	    /* @var $oReq Request */
	    $oSrvMessenger = &$this->_oModule->getService('Messenger');
        // validator
        $aFields = array(
							'aMail[user]'  => array('title'=>App::format('Mod_Messenger.member'),  'def'=>'required',  'optional'=>true),
				            'aMail[title]' => array('title'=>App::format('Mod_Messenger.subject'), 'def'=>'required'),
				            'aMail[text]'  => array('title'=>App::format('Mod_Messenger.message'), 'def'=>'required'),
                        );
        $oValidator =  &App::newObject('Validator', $aFields);
        $aShip = array('none'=> '-----------', 'all'=>App::format('Mod_Messenger.all_members'));

        foreach ($oSrvMessenger->getShipToSelect() as $i => $sShip)
        {
			if ($sShip == "Guest")
			{
				continue;
			}
			$i = ($i == '0' ? '__admin__' : $i);
        	$aShip[$i] = $sShip;
        }
        $aPost = array();
        if($oReq->getAction() == 'SendMail')
        {
            $aPost = $oReq->getArray('aMail');

            if(!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            else
            {
                if($oReq->getInt('iCurPage'))
                {               	
                	list($iTotal, $iFrom, $iTo) = $oSrvMessenger->sendMassMail($aPost, $oReq->getInt('iCurPage'));

                    $bNext = $oReq->getInt('iCurPage')+1;
                    $aErrors = $oSrvMessenger->getErrors();

                    if(($iFrom > $iTotal) || (!$iFrom && !$iTo))
                        App::gotoUrl('admin.extra.massmail', array(), !empty($aErrors) ? $aErrors : array(App::format('Mod_messenger.massmail.completed')), !empty($aErrors) ? true :false, 'MassMail');
                    else
                        $bProc = true;

                    $iFrom = (!$iFrom) ? 1 : $iFrom+1;
                }
            }
        }
        $oTpl = &$this->_oModule->getTemplate();

        $oTpl->assignSrc(array(
                                'aShip'         => $aShip,
                                'jsValidation'  => $oValidator->makeJS(),
		                        'jsValMess'     => $oValidator->makeJsMess(),
                                'aErrors'		=> $this->getErrors(),
                                'aMess'         => (!empty($_SESSION['_messages']['admin.extra.massmail'])) ? 1 :0,
                                'bProc'         => $bProc,
                                'iCurPage'      => !empty($bNext) ? $bNext : 1,
                                'iFrom'         => !empty($iFrom) ? $iFrom : 0,
                                'iTo'           => $iTo,
                                'iTotal'        => !empty($iTotal) ? $iTotal : '',
                                'aMail'         => $aPost,
                                'aRound'        => array(
                                                            10=>App::format('Mod_Messenger.per_round', '10'),
                                                            20=>App::format('Mod_Messenger.per_round', '20'),
                                                            30=>App::format('Mod_Messenger.per_round', '30'),
                                                            40=>App::format('Mod_Messenger.per_round', '40'),
                                                            50=>App::format('Mod_Messenger.per_round', '50'),
                                                            0 =>App::format('Mod_Messenger.send_one_time')
                                                        ),
		));
		$srcHtml = $oTpl->fetch('MassMail.html');
		return $srcHtml;
	}
}
?>